/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.repository.model.evaluation;

import cn.ac.nercis.pes.common.utils.JsonUtils;
import cn.ac.nercis.pes.repository.constant.BaseConstants;
import cn.ac.nercis.pes.repository.model.base.BaseEntity;
import jakarta.persistence.*;
import lombok.*;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.GenericGenerator;


/**
 * 资产属性信息实体类
 * @author zpy
 * @version 1.0.0
 */
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "pes_asset_attribute")
public class AssetAttributeDO extends BaseEntity {
    @Id
    @GenericGenerator(name = "generator",strategy = BaseConstants.CUSTOM_UUID_STRATEGY)
    @GeneratedValue(generator = "generator")
    @Column(name = "id",unique = true,nullable = false,length = 36)
    private String id;

    @Column(name = "attribute_id",nullable = false,length = 36)
    private String attributeId;

    @Column(name = "field_name",nullable = false,length = 128)
    private String fieldName;

    @Column(name = "display_name",nullable = false,length = 128)
    private String displayName;

    @Column(name = "content", length = 2000)
    private String value;

    @Column(name = "sort",nullable = false,length = 36)
    private Integer sort;

    @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
    @JoinColumn(name = "asset_id")
    private AssetDO asset;

    @Override
    public String toString() {
        return this.getClass().getSimpleName().concat(JsonUtils.toJson(this));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (!(o instanceof AssetAttributeDO that)) {
            return false;
        }

        return new EqualsBuilder()
                .append(getCreateTime(),that.getCreateTime())
                .append(getModifyTime(),that.getModifyTime())
                .append(getDeleteFlag(),that.getDeleteFlag())
                .append(id, that.id)
                .append(attributeId, that.attributeId)
                .append(fieldName, that.fieldName)
                .append(displayName, that.displayName)
                .append(value, that.value)
                .append(sort, that.sort)
                .append(asset, that.asset)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(getCreateTime())
                .append(getModifyTime())
                .append(getDeleteFlag())
                .append(id)
                .append(attributeId)
                .append(fieldName)
                .append(displayName)
                .append(value)
                .append(sort)
                .append(asset)
                .toHashCode();
    }
}